﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OutdoorLib.BaseEntity.Agency;
using OutdoorLib.BaseEntity.WS;
using Grishko.Tools;

public partial class DiscountListEditor : MasterPage_EditControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
            MyDataBind();

        SetAlertForTextBox();

    }

    private void MyDataBind()
    {                
        DataBind();
    }
        
    private Discount _Discount = null;
    protected Discount Discount
    {
        get
        {
            if (null != _Discount)
                return _Discount;

            if (GlobalConstants.IsNewRecord(CurrentID))
                _Discount = (Discount)AppEnvironment.ObjectFactory.GetNewObject(CurrentTagName, GlobalConstants.GetNewID(CurrentID));
            else
                _Discount = (Discount)AppEnvironment.ObjectFactory.GetObject(CurrentID, CurrentTagName);

            return _Discount;
        }

    }
    protected void ReadForm()
    {
        Discount.Name = txtName.Text ;
        Discount.dtStart = Convert .ToDateTime (txt_dtStart.Text  );
        Discount.dtFinish    = Convert.ToDateTime(txt_dtFinish.Text);
        Discount.IsCharge = rbIsCharge.Checked;
        Discount.Percent = Convert.ToInt16(txtPercent.Text);


    }
    protected void btnSaveCommonInfo_Click(object sender, EventArgs e)
    {
        ReadForm();
        Save();
        HideAlert();

    }

    private bool Save()
    {        
        try
        {
            // Если эта новая запись - id убрать префикс новой записи
            if (GlobalConstants.IsNewRecord(Discount .ID ))
                Discount.ID = CurrentID;

            // добавить сотрудника в агентсво
            AppEnvironment.CurrentAgency.Discounts.Remove(Discount.ID);
            AppEnvironment.CurrentAgency.Discounts.Add(Discount);
            //сохранить агентсво с эсколацией save  
            wsAgency.SaveAgency();

            // почистить кэш 
            AppEnvironment.ObjectFactory.ResetCashe(GlobalConstants.GetNewID(CurrentID));

            

            return true;
        }
        catch
        {
            return false;
        }
    }
    protected void btnSaveExit_Click(object sender, EventArgs e)
    {
        btnSaveCommonInfo_Click(null, null);
        QueryStringTools.RefreshPageWitchReturnUrl();
    }
}
